package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.SystemAdminLogManager;
import com.fish.anne.model.SystemAdminLog;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class SystemAdminLogAction extends BaseAction implements Preparable {
    private SystemAdminLogManager systemAdminLogManager;
    private List systemAdminLogs;
    private SystemAdminLog systemAdminLog;
    private Long  logId;

    public void setSystemAdminLogManager(SystemAdminLogManager systemAdminLogManager) {
        this.systemAdminLogManager = systemAdminLogManager;
    }

    public List getSystemAdminLogs() {
        return systemAdminLogs;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String systemAdminLogId = getRequest().getParameter("systemAdminLog.logId");
            if (systemAdminLogId != null && !systemAdminLogId.equals("")) {
                systemAdminLog = systemAdminLogManager.get(new Long(systemAdminLogId));
            }
        }
    }

    public String list() {
        systemAdminLogs = systemAdminLogManager.getAll();
        return SUCCESS;
    }

    public void setLogId(Long  logId) {
        this. logId =  logId;
    }

    public SystemAdminLog getSystemAdminLog() {
        return systemAdminLog;
    }

    public void setSystemAdminLog(SystemAdminLog systemAdminLog) {
        this.systemAdminLog = systemAdminLog;
    }

    public String delete() {
        systemAdminLogManager.remove(systemAdminLog.getLogId());
        saveMessage(getText("systemAdminLog.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (logId != null) {
            systemAdminLog = systemAdminLogManager.get(logId);
        } else {
            systemAdminLog = new SystemAdminLog();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (systemAdminLog.getLogId() == null);

        systemAdminLogManager.save(systemAdminLog);

        String key = (isNew) ? "systemAdminLog.added" : "systemAdminLog.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}